# Import the Dataset model
from myapp.models.model_dataset import Dataset
# Import the exception logging utility
from myapp.utils.exception import log_exception


# Define a function to generate a new version name for a dataset
def gen_version_name(dataset_id, session):
    """
    生成新的数据集版本名称
    """
    # Query the database for the latest version name of the given dataset ID
    latest_dataset_version = session.query(Dataset.name).filter(
        Dataset.dataset_id == dataset_id).order_by(
        Dataset.created_on.desc()).first()
    # Initialize the new version name to 'v1'
    new_dv_name = 'v1'
    # If a latest version exists, generate a new version name
    if latest_dataset_version:
        # Use a try-except block to handle potential exceptions
        with log_exception:
            # Increment the version number and create the new version name
            new_dv_name = 'v' + str(int(latest_dataset_version[0][1:]) + 1)
    # Return the new version name
    return new_dv_name